﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.OMM.HealthMonitor.Entity.Common;

namespace Sinacor.OMM.HealthMonitor.Entity.Receiver
{
    [Serializable()]
    public class STMConnection : IHealthDataReset
    {

        HealthEntityBase _healthEntityBase;

        public STMConnection()
        {
            _healthEntityBase = new HealthEntityBase();
        }

        public STMConnection(HealthEntityBase healthEntityBase)
        {
            _healthEntityBase = healthEntityBase;
        }

        // Estado bom para ficar Conectado ao STM
        bool _goodStateConnected = false;
        public bool GoodStateConnected
        {
            get { return _goodStateConnected; }
            set 
            { 
                _goodStateConnected = value;
                _healthEntityBase.LastUpdate = DateTime.Now;
            }
        }

        // Conectado ao STM
        bool _connected = false;
        public bool Connected
        {
            get { return _connected; }
            set 
            { 
                _connected = value;
                _healthEntityBase.LastUpdate = DateTime.Now;
            }
        }

        //Número de eventos de erro disparado pelo STM
        private long _errorEventsQuantity = -1;
        public long ErrorEventsQuantity
        {
            get { return _errorEventsQuantity; }
            set 
            { 
                _errorEventsQuantity = value;
                _healthEntityBase.LastUpdate = DateTime.Now;
            }
        }

        //Número de tentativas de conexão com o STM bem sucedidas
        private long _successfulConnectionsQuantity = -1;
        public long SuccessfulConnectionsQuantity
        {
            get { return _successfulConnectionsQuantity; }
            set 
            { 
                _successfulConnectionsQuantity = value;
                _healthEntityBase.LastUpdate = DateTime.Now;
            }
        }

        //Número de tentativas de conexão com o STM mau sucedidas
        private long _unsuccessfulConnectionsQuantity = -1;
        public long UnsuccessfulConnectionsQuantity
        {
            get { return _unsuccessfulConnectionsQuantity; }
            set 
            { 
                _unsuccessfulConnectionsQuantity = value;
                _healthEntityBase.LastUpdate = DateTime.Now;
            }
        }

        //Tempo médio para envio do ACK para o STM
        private TimeSpan _averageTimeToCallAck = new TimeSpan();
        public String AverageTimeToCallAck
        {
            get { return _averageTimeToCallAck.ToString(); }
            set
            {
                _averageTimeToCallAck = TimeSpan.Parse(value);
                _healthEntityBase.LastUpdate = DateTime.Now;
            }
        }

        //Tempo médio para o envio do ACK para o STM
        private TimeSpan _limitAverageTimeToCallAck = TimeSpan.MinValue;
        public String LimitAverageTimeToCallAck
        {
            get { return _limitAverageTimeToCallAck.ToString(); }
            set
            {
                _limitAverageTimeToCallAck = TimeSpan.Parse(value);
                _healthEntityBase.LastUpdate = DateTime.Now;
            }
        }

        //Tempo médio entre receber a mensagem e o envio do ACK para o STM
        private TimeSpan _averageTimeToSendAck = new TimeSpan();
        public String AverageTimeToSendAck
        {
            get { return _averageTimeToSendAck.ToString(); }
            set 
            { 
                _averageTimeToSendAck = TimeSpan.Parse(value);
                _healthEntityBase.LastUpdate = DateTime.Now;
            }
        }

        //Tempo médio entre receber a mensagem e o envio do ACK para o STM
        private TimeSpan _limitAverageTimeToSendAck = TimeSpan.MinValue;
        public String LimitAverageTimeToSendAck
        {
            get { return _limitAverageTimeToSendAck.ToString(); }
            set 
            { 
                _limitAverageTimeToSendAck = TimeSpan.Parse(value);
                _healthEntityBase.LastUpdate = DateTime.Now;
            }
        }

        public void Reset()
        {
            Connected = false;
            ErrorEventsQuantity = 0;
            SuccessfulConnectionsQuantity = 0;
            UnsuccessfulConnectionsQuantity = 0;
        }
    }
}
